package com.week3.day02.atrShow;

import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;

public class ArtShowServiceImpl implements ArtShowService {

    private List<ArtShow> data = ArtShowUtils.createList();

    public ArtShowServiceImpl() throws FileNotFoundException {
    }

    @Override
    public boolean addArtShow(String studentName) {
        boolean flag = true;
        for (ArtShow item : data) {
            if (item.getStuName().equals(studentName)) {
                item.setCount(item.getCount() + 1);
                if(item.getCount()>0){  // 如果有表演次数，那么status改成0
                    item.setStatus(0);
                }
                flag = false;
                break;
            }
        }
        if(flag){  // 没有重名的新的学生添加到list中
            ArtShow artShow = new ArtShow(studentName, "唱歌", 0, 1);
            data.add(artShow);
        }
        return true;
    }

    /**
     *
     * @return null代表所有学生都表演完毕
     */
    @Override
    public String pickStudent() {

        for (ArtShow artShow : data) {
            if (artShow.getStatus().equals(0)){
                String stuName = artShow.getStuName();
                return stuName;
            }
        }

        return null;
    }

    @Override
    public boolean updateArtShow(ArtShow artShow) {
        for (ArtShow item : data) {
            if (item.getStuName().equals(artShow.getStuName())) {
                if (item.getCount() > 0) {
                    item.setCount(item.getCount() - 1);
                }
                if (item.getCount().equals(0)) {
                    item.setStatus(1);
                }
                break;
            }
        }
        return false;
    }

    @Override
    public List<ArtShow> getAll() {
        return data;
    }

    @Override
    public List<String> getNoShowNames() {
        List<String> noList = new LinkedList<String>();
        for (ArtShow item :
                data) {
            if (item.getStatus().equals(0)) {
                noList.add(item.getStuName());
            }
        }
        return noList;
    }
}
